package servlet;

import bean.SignalEntity;
import bean.signal_path;
import com.google.gson.Gson;
import com.jmatio.io.MatFileReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class DownLoadSignalDataServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(new Date()) + ":DownloadSignalDate");
        List<signal_path> signalPathList = parse(req.getInputStream());
        resp.getOutputStream();

    }
    private void processResp(OutputStream out) throws IOException {
            MatFileReader m = new MatFileReader("/Volumes/mac_resource/case_west/B:S/Bear_Project/Bear_BS/data/12k_Drive_End_B007_0_118.mat");
    }

    private List<signal_path> parse(InputStream in) throws IOException {
        List<signal_path> signal_paths = new LinkedList<>();
        Gson gson = new Gson();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line = null;
        while((line=br.readLine())!=null)
        {
            line = line.substring(1,line.length()-1);
        }
        String[] lines = line.split(",");
        signal_path s = null;
        for(int i=0;i<lines.length;i++)
        {
            s = gson.fromJson(lines[i],signal_path.class);
            signal_paths.add(s);
        }
        return signal_paths;
    }
}
